import 'package:equatable/equatable.dart';
import 'package:excel_utils/pages/daily/daily_report/bean/CustomerBean.dart';
import 'package:excel_utils/pages/week/bean/DisCustomerRecordBean.dart';
import 'package:excel_utils/repository/k8/k8_repository.dart';
import 'package:excel_utils/repository/week/dis_customer_record_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'dis_customer_record_state.dart';

class DisCustomerRecordCubit extends Cubit<DisCustomerRecordState> {
  DisCustomerRecordRepository disCustomerRecordRepository;
  K8Repository _k8Repository;
  DisCustomerRecordCubit(this.disCustomerRecordRepository, this._k8Repository) : super(DisCustomerRecordInitial());

  refresh() async {
    emit(DisCustomerRecordRefreshing(timestamp: DateTime.now().millisecondsSinceEpoch));
  }

  saveDisCustomerRecord({required List<DisCustomerRecordBean> disCustomerRecordData}) async {
    emit(DisCustomerRecordSaveLoading());
    List<DisCustomerRecordBean> quotationRecordData = await disCustomerRecordRepository.saveDisCustomerRecordData(disCustomerRecordData: disCustomerRecordData);
    emit(DisCustomerRecordSaveSuccessState(disCustomerRecordData: disCustomerRecordData));
  }

  getDisCustomerRecord({maps}) async {
    emit(DisCustomerRecordDataLoading());
    List<DisCustomerRecordBean> disCustomerRecordData = await disCustomerRecordRepository.getDisCustomerRecordData(maps: maps);
    emit(DisCustomerRecordDataSuccessState(disCustomerRecordData: disCustomerRecordData));
  }

  delDisCustomerRecord({required String disCustomerRecordId}) async {
    emit(DisCustomerRecordDeleteLoading(disCustomerRecordId: disCustomerRecordId));
    String deleteQuotationRecordId =
        await disCustomerRecordRepository.deleteDisCustomerRecordData(disCustomerRecordId: disCustomerRecordId);
    if (deleteQuotationRecordId != "-1") {
      emit(DisCustomerRecordDeleteSuccessState(disCustomerRecordId: disCustomerRecordId));
    } else {
      emit(DisCustomerRecordDeleteFailureState(message: "删除失败", disCustomerRecordId: disCustomerRecordId));
    }
  }

  // 根据编号查询客户信息
  searchCustomer({required String? customerNo}) async {
    emit(DisCustomerRecordSearchCustomerLoading(customerNo: customerNo.toString()));
    // 调用接口查询客户信息
    CustomerBean customerBean = await _k8Repository.queryCustomerData(maps: {"cmCode": customerNo});
    print(customerBean.data);
    if (customerBean.status == 1 && customerBean.data != null) {
      emit(DisCustomerRecordSearchCustomerSuccessState(customerNo: customerNo.toString(), customerBean: customerBean));
    }
    // emit(MonthlyPackageTransferSearchCustomerSuccessState(customerNo:customerNo));
  }
}
